from coin.exchange.base.kr_rest.currency import NativeCurrency
from coin.exchange.base.kr_rest.product import NativeProduct, NormProduct
from coin.exchange.bitfinex_v2.kr_rest.constants import currency_list, quote_currency_list
from coin.exchange.bitfinex_v2.config import group_symbol
from coin.exchange.bitfinex_v2.kr_rest.currency import BitfinexCurrency


class BitfinexProduct(NativeProduct):
  _exchange = "Bitfinex"
  NativeCurrencyType = BitfinexCurrency

  def __init__(self, base, quote):
    assert base.currency in currency_list, base
    assert quote.currency in quote_currency_list, quote
    assert base != quote, (base, quote)
    super().__init__(base, quote, "t%s%s" % (base.native_symbol, quote.native_symbol))
    self._native_subscription_symbol = "%s%s" % (base.native_symbol, quote.native_symbol)

  @classmethod
  def FromStrProductImpl(cls, product, current_datetime):
    assert isinstance(product, str)
    if "-" in product:
      ret = product.split("-")
      assert len(ret) == 2
      base = BitfinexCurrency.FromStrCurrency(ret[0])
      quote = BitfinexCurrency.FromStrCurrency(ret[1])
      return BitfinexProduct(base, quote)
    raise ValueError('Invalid product %s' % product)

  @classmethod
  def FromStrNativeProductImpl(cls, product, current_datetime):
    assert isinstance(product, str)
    if len(product) == 6:
      return BitfinexProduct(BitfinexCurrency.FromStrNativeCurrency(product[0:3]),
                             BitfinexCurrency.FromStrNativeCurrency(product[3:6]))
    elif product[0] == 't':
      product = product[1:]
      return BitfinexProduct(BitfinexCurrency.FromStrNativeCurrency(product[0:3]),
                             BitfinexCurrency.FromStrNativeCurrency(product[3:6]))
    raise ValueError('Invalid native product %s' % product)

  @classmethod
  def FromStrNativeProductV1(cls, product):
    assert isinstance(product, str)
    assert len(product) == 6, product
    return BitfinexProduct(BitfinexCurrency.FromStrNativeCurrencyV1(product[0:3]),
                           BitfinexCurrency.FromStrNativeCurrencyV1(product[3:6]))
